var API = {
    params: {
        /**matomo API接口必须参数 */
        "token_auth": piwik.token_auth,
        "force_api_session": 1, 
        "format": 'json'
    },
    _getFormData: function (param) {
        var formData = new FormData();
        for (var p in param) {
            //处理 值为对象 时再循环一次 变成PHP格式数组
            var first = param[p] ;
            if (first === 'null' || first == null || first == undefined || first === 'undefined') {
                formData.append(p, "");
            }
           // console.log(p, typeof first )
            else if (typeof first === 'object'){
                //发送 数组 例如 images:[{url:'', name:''}]
               if(first.length>0){
                    for (var j in first) {
                    
                        if (typeof first[j] === 'object') {
                            for (var k in first[j]) {
                                formData.append(p + '[' + j + '][' + k + ']', first[j][k] );
                            }
                        }else{
                            formData.append(p, first);
                        }
                    }
                }else{
                    // 发送对象为数组 例如 file:{ name:'', url:''}
                    for (var i in first) {
                        formData.append(p + '[' + i + ']', API._filter_value(first[i]));
                    }
                }
             
            }else{
                formData.append(p, API._filter_value(first));
            }
           
        }
        return formData;
    },
    // 把一维数组作为‘,’拼接的字符串
    _filter_value(value){
        if(typeof value === 'Array'){
            return value.join(',');
        } else if (value !== undefined){
            return value;
        }else{
            return "";
        }
        
    },
    // 发送axios请求
    _axiosPost: function (url, param) {
        axios.defaults.withCredentials = true;
        return new Promise((resolve, reject) => {
            axios.post(url,
                API._getFormData(param)
            ).then((response) => {
                resolve(response.data);
            }).catch(ret => {
                reject(ret);
            })
        })
    }, 
    //保猎鹰浏览器IOS参数设置参数
    saveLybrowerJson: function (formData) {
        var param = Object.assign(formData, API.params);
        return API._axiosPost('index.php?module=API&method=AdsPlace.saveLybrowerJson', param);
    },
    getLybrowerjson: function () {
        var param = Object.assign({ 'xfjson': 1 }, API.params);

        return API._axiosPost('index.php?module=API&method=AdsPlace.getLybrowerjson', param);
    },
    //保存影音先锋广告参数设置参数
    xfjsonSave: function (formData){
        var param = Object.assign(formData, API.params);
        return API._axiosPost('index.php?module=API&method=AdsPlace.xfjsonSave', param); 
    } ,
    // 获取 影音先锋 参数
    xfjsonGet: function () {
        var param = Object.assign({'xfjson':1}, API.params);

        return API._axiosPost('index.php?module=API&method=AdsPlace.xfjsonGet', param);
    },
    //保存检测redis、mysql、pgsql的设置参数
    xfdetection: function (formData){
        var param = Object.assign(formData, API.params);

        return API._axiosPost('index.php?module=API&method=AdsPlace.xfdetection', param);
    },
    xfdetectionGet: function(){
        var param = Object.assign({ 'xfjson': 1 }, API.params);
        return API._axiosPost('index.php?module=API&method=AdsPlace.xfdetectionGet', param);
    },
    //检测某个类型的 链接状态
    xfdetectionTesting(  formData) {
        var param = Object.assign(formData, { format: "json" });
        return API._axiosPost('index.php?module=AdsPlace&action=detection', param);
    },
    //formData = {testtype:'redis',ipstr:'127.0.0.1@6379' }
    xfpingTesting( formData){
        var param = Object.assign(formData, { format: "json" });
        return API._axiosPost('index.php?module=AdsPlace&action=ping', param);
    },
    //创建或者更新 广告位数据 
    createAdPlace: function( formData )
    {
        var param = Object.assign(formData, API.params);
        //================
        if(formData.id>0){
            // 编辑信息
            return API._axiosPost('index.php?module=API&method=AdsPlace.updatePlace', param); 
        }else{
            return API._axiosPost('index.php?module=API&method=AdsPlace.createPlace', param); 
        }
        
    },
    // 获取广告位列表
    getAllsPlace: function (page, pageSize, queryParams) {
        var param = Object.assign({
            "page": page,
            "pageSize": pageSize,
            "segment": queryParams,
        }, API.params);

        return API._axiosPost('index.php?module=API&method=AdsPlace.getAllsPlace', param);
    },
    // 获取一条 广告位 
    getOnePlace: function (idPlace) {
        var param = Object.assign({
            "idPlace": idPlace,            
        }, API.params);
        return API._axiosPost('index.php?module=API&method=AdsPlace.getOnePlace', param);  
    },
    // 删除一个广告位
    deletePlace: function (idPlace) {
        var param = Object.assign({
            "idPlace": idPlace,
        }, API.params);
        return API._axiosPost('index.php?module=API&method=AdsPlace.deletePlace', param);   
    },
    //改变一个广告位的状态 disabed 值 idPlaces = [], value = 0,1,2,3,4,
    changeDisableStatus: function (idPlaces, value){
        var param = Object.assign({
            "idPlaces": idPlaces ,
            "disabled": value,
        }, API.params);
        return API._axiosPost('index.php?module=API&method=AdsPlace.changeDisableStatus', param);  
    },
    // 创建一个广告内容 存数据库接口
    createAdverte: function (formData) {
        // 处理参数
        var param = Object.assign(formData, API.params);
        //================
        if (formData.id > 0) {
            // 编辑信息
            return API._axiosPost('index.php?module=API&method=AdsPlace.updateAdverte', param);
        } else {
            return API._axiosPost('index.php?module=API&method=AdsPlace.createAdverte', param);
        }

    },
    // 获取所有的投放广告 分页获取
    getAllsAdvertes(page, pageSize, queryParams){
        var param = Object.assign({
            "page": page,
            "pageSize": pageSize,
            "segment": queryParams,
        }, API.params);

        return API._axiosPost('index.php?module=API&method=AdsPlace.getAllsAdvertes', param);
    },
    deleteAdverte(id){
        var param = Object.assign({
            "idadverte": id,
        }, API.params);
        return API._axiosPost('index.php?module=API&method=AdsPlace.deleteAdverte', param); 
    },
    // 创建一个广告内容 存数据库接口
    createMedias: function (formData) {
        // 处理参数
        var param = Object.assign(formData, API.params);
        //================
        if (formData.id > 0) {
            // 编辑信息
            return API._axiosPost('index.php?module=API&method=AdsPlace.updateMedias', param);
        } else {
            return API._axiosPost('index.php?module=API&method=AdsPlace.createMedias', param);
        }

    },
    // 分页获取 按条件搜索物料
    getAllMedias(page, pageSize, queryParams) {
        var param = Object.assign({
            "page": page,
            "pageSize": pageSize,
            "segment": queryParams,
        }, API.params);

        return API._axiosPost('index.php?module=API&method=AdsPlace.getAllMedias', param);
    },
    // 通过id 删除一个 物料
    deleteMedias(id) {
        var param = Object.assign({
            "idmedias": id,
        }, API.params);
        return API._axiosPost('index.php?module=API&method=AdsPlace.deleteMedias', param);
    },
    getMedias(id){
        var param = Object.assign({
            "idmedias": id,
        }, {format:"json"});
        return API._axiosPost('index.php?module=AdsPlace&action=getmedias', param);  
    }
}
// exports.API = API;
 export default API;